package com.xiaotu.spider.project.pipeline;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.xiaotu.common.model.BaiduNewsModel;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.BaiduNewsStorageProcessor;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

@Component("BaiduNewsPipeline")
@Scope("prototype")
public class BaiduNewsPipeline extends APipeline{
	
	private final String fileName = BaiduNewsPipeline.class.getResource("").getPath() + File
            .separator + System.currentTimeMillis() + ".csv";
	
	@Override
	protected void doProcess(ResultItems resultItems, Task task) {
		BaiduNewsModel tv = resultItems.get(BaiduNewsModel.class.getName());
        if (tv != null)
            this.writeFile(tv);
        List<BaiduNewsModel> tvList = resultItems.get(BaiduNewsModel.class.getName() + "List");
        if (tvList != null)
            tvList.forEach(tvModel -> this.writeFile(tvModel));
	}
	private void writeFile(BaiduNewsModel tv)
    {
        JSONObject tvObj = JSONObject.fromObject(tv);
        List<Object> list = new ArrayList<>();
        Arrays.stream(BaiduNewsStorageProcessor.FIELD_ARR).limit(BaiduNewsStorageProcessor.FIELD_ARR
                .length - 1).forEach(field -> list.add(tvObj.get(field)));
        FileUtil.writeFile(fileName, StringUtil.list2CSVLine(list, SepratorUtil.SEP_COMMA_EN) +
                SepratorUtil.LINE_SEPRATOR);
    }
}
